/**
  ******************************************************************************
  * @file    communi_print.c
  * @author  Sherlock
  * @version V0.1
  * @date    09/5/2024
  * @brief   Communication print function.
  ******************************************************************************
  * @copy
  *
  * <h2><center>&copy; COPYRIGHT 2010 Feirui</center></h2>
  */ 



/**
  * @brief  INCLUDE FILES.
  */
#ifndef	COMMUNI_PRINT_GLOBAL
#define	COMMUNI_PRINT_GLOBAL
#include <includes.h>
#include "communi_print.h"
#include "communi_server.h"


static void CommPrintDataCompose(uint32_t* SourceList, uint8_t sourceLen, uint8_t* SourceTitle, uint8_t* TargetSendList, uint16_t* length);

uint8_t App_StartPrint(void)
{
  uint8_t  CommDataStatus=CommDealReturnCode_Null;
  uint16_t testSendLen;
//   switch (CommPrintConnectType)
//   {
//     case CommTypeOrd_USB:
      CommPrintDataCompose((uint32_t*)CommPrintBuffer, CommPrintDataNum, CommData_PrintCurrentQueue, USBTXBufferData, &testSendLen);
      USBDateSendCOMM_handling(USBTXBufferData, (uint16_t)testSendLen);
//       break;
//     case CommTypeOrd_BTH:
//       CommPrintDataCompose(CommPrintBuffer, CommPrintDataNum, CommData_PrintCurrentQueue, BTHTXBufferData, &testSendLen);
//       BTHDataSendCOMM_handling(BTHTXBufferData, (uint16_t)testSendLen);
//       break;
//     default:
//       break;
//   }
  return(CommDataStatus);
}


static void CommPrintDataCompose(uint32_t* SourceList, uint8_t sourceLen, uint8_t* SourceTitle, uint8_t* TargetSendList, uint16_t* targetLen)
{
	TargetSendList[0]=(uint8_t)GetDataBaseDataContent(FakeConfig_ModuleID);
	TargetSendList[1]=COMMCommandType_Cfg40;
	uint16_t TempDataLen = (uint16_t)sourceLen*CommPrintFrameMinLen*CommPTCByteSize_PrintPack;
	TargetSendList[2]=(TempDataLen>>8)&0x00ff;
	TargetSendList[3]=TempDataLen     &0x00ff;

	for(uint8_t j = 0; j < CommPrintFrameMinLen; j++)
	{
		for(uint8_t i = 0; i < sourceLen; i++)
		{
			TargetSendList[(uint16_t)CommPTCByteSize_STRPack+(i+j*sourceLen)*CommPTCByteSize_PrintPack+0]=SourceTitle[i];
			TargetSendList[(uint16_t)CommPTCByteSize_STRPack+(i+j*sourceLen)*CommPTCByteSize_PrintPack+1]=(SourceList[i+j*sourceLen]   )&0x00ff;
			TargetSendList[(uint16_t)CommPTCByteSize_STRPack+(i+j*sourceLen)*CommPTCByteSize_PrintPack+2]=(SourceList[i+j*sourceLen]>>8)&0x00ff;
			TargetSendList[(uint16_t)CommPTCByteSize_STRPack+(i+j*sourceLen)*CommPTCByteSize_PrintPack+3]=(SourceList[i+j*sourceLen]>>16)&0x00ff;
			TargetSendList[(uint16_t)CommPTCByteSize_STRPack+(i+j*sourceLen)*CommPTCByteSize_PrintPack+4]=(SourceList[i+j*sourceLen]>>24)&0x00ff;
		}
	}

	uint16_t CRCChecksum=CalBufferDataCRC161(TargetSendList, CommPTCByteSize_STRPack+sourceLen*CommPrintFrameMinLen*CommPTCByteSize_PrintPack);
	TargetSendList[(uint16_t)CommPTCByteSize_STRPack+sourceLen*CommPrintFrameMinLen*CommPTCByteSize_PrintPack+0]= CRCChecksum    &0x00ff;
	TargetSendList[(uint16_t)CommPTCByteSize_STRPack+sourceLen*CommPrintFrameMinLen*CommPTCByteSize_PrintPack+1]=(CRCChecksum>>8)&0x00ff;
	TargetSendList[(uint16_t)CommPTCByteSize_STRPack+sourceLen*CommPrintFrameMinLen*CommPTCByteSize_PrintPack+2]='\0';
	*targetLen = (uint16_t)CommPTCByteSize_STRPack+sourceLen*CommPrintFrameMinLen*CommPTCByteSize_PrintPack+2;
}


void CommPreparePrintData(void)
{
	if(CommPrintDataNum)  //当前允许打印才进入
	{
		for(uint8_t i = 0; i < CommPrintDataNum; i++)  //填装数据
		{
			CommPrintBuffer[CommPrintFrameCurrentIndex*CommPrintDataNum+i] = CommData_PrintTotalQueue[CommData_PrintCurrentQueue[i]];
		}
		CommPrintFrameCurrentIndex++;

		if(CommPrintFrameCurrentIndex == CommPrintFrameMinLen) //保存十帧数据一起发送，通知COMM进行打印
		{
			CommPrintFrameCurrentIndex = 0;  //及时清空，以免之前未打印完成，造成下一次重复打印

			uint8_t os_err;
    		OSFlagPost(App_Flags_COMMUNI, OSCOMMUNIFlags_TXStartPrint, OS_FLAG_SET, &os_err);
		}
  	}
}


#endif	/*	COMMUNI_PRINT_GLOBAL	*/
